<%@ include file="/common/taglibs.jsp"%>

<head>
    <title><fmt:message key="articulolist.title"/></title>
    <meta name="heading" content="<fmt:message key='articulolist.heading'/>"/>    
</head>

<form:form id="articuloList">

<c:set var="buttons">
    <input type="button" class="button" 
        onclick="location.href='<c:url value="/articulo/articuloform.html?method=Add&from=list"/>'"
        value="<fmt:message key="button.add"/>"/>
        
    <input type="button" class="button" 
    	onclick="location.href='<c:url value="/mainMenu.html"/>'"
        value="<fmt:message key="button.done"/>"/>
</c:set>

<ul>
	<li>
	<label class="desc"><fmt:message key="common.filtros.busqueda"/></label>
	<div class="group" style="width: 300px">
		<div>
			<fmt:message key="common.articulo"/><br/>
			<input type="text" id="deArticulo" name="deArticulo" class="text medium"/>								
		</div><br/>	
		<div>
			<fmt:message key="common.codigo"/><br/>
			<input type="text" id="coCodigoArticulo" name="coCodigoArticulo" class="text medium"/>			
		</div><br/>
		<div>
			<fmt:message key="common.categoria"/><br/>
			<select id="coCategoria" name="coCategoria" class="text medium">
				<option value="0"><fmt:message key="label.itemdefault"/></option>
				<c:forEach var="categoria" items="${categorias}" varStatus="status">	
					<option value="${categoria.id}">${categoria.deCategoria}</option>
				</c:forEach>			
			</select>
		</div><br/>						
		<div>
			<fmt:message key="common.maneja.stock"/><br/>
			<select id="stManejaStock" name="stManejaStock" class="text medium">
				<option value="-"><fmt:message key="label.itemdefault"/></option>
				<option value="PC"><fmt:message key="common.maneja.stock.punto.canje"/></option>
				<option value="AP"><fmt:message key="common.maneja.stock.almacen.principal"/></option>
				<option value="NM"><fmt:message key="common.no.maneja.stock"/></option>
				<option value="IN"><fmt:message key="common.maneja.premioinstantaneo"/></option>
				<option value="IV"><fmt:message key="common.maneja.premioinstantaneovale"/></option>
			</select>
		</div><br/>				
		<div>
			<fmt:message key="label.habilitado"/><br/>
			<select id="stArticulo" name="stArticulo" class="text medium">
				<option value="-"><fmt:message key="label.itemdefault"/></option>
				<option value="Y"><fmt:message key="common.yes"/></option>
				<option value="N"><fmt:message key="common.no"/></option>
			</select>
		</div><br/><br/>		
		<div>
			<input type="button" onclick="buscarArticulos()" value="<fmt:message key="button.search"/>"/>
		</div>					
	</div>	
	</li>
</ul>

<c:out value="${buttons}" escapeXml="false" /><br/><br/>
<table id="articulos" cellpadding="0" class="table" cellspacing="0" style="width: 800px" > 
	<thead> 
		<tr> 
            <th scope="col"><fmt:message key="common.articulo"/></th>
            <th scope="col"><fmt:message key="common.codigo.articulo"/></th>
            <th scope="col"><fmt:message key="common.categoria"/></th>
            <th scope="col"><fmt:message key="common.maneja.tipoarticulo"/>?</th>            
            <th scope="col"><fmt:message key="label.habilitado"/></th>		
		</tr>
	</thead> 
	<tbody id="articulosbody">
		<tr id="pattern" style="display:none;">
			<td><span id="tableArticulo">Articulo</span></td>
			<td><span id="tableCodigo">Codigo</span></td>	
			<td><span id="tableCategoria">Categoria</span></td>	
			<td><span id="tableStock">Stock</span></td>						
			<td><span id="tableHabilitado">Habilitado</span></td>											
		</tr> 	
		<c:forEach var="articulo" items="${articulos}" varStatus="status">	
			<c:choose>
				  <c:when test="${status.index % 2 == 0}"><tr class="odd" ></c:when>
				  <c:otherwise><tr class="even" ></c:otherwise>
			</c:choose>
            	<td>
					<a href="<c:url value="/articulo/articuloform.html?id=${articulo.id}&amp;from=list"/>">					
						${articulo.deArticulo}
					</a>            	
				</td>
                <td>
                    ${articulo.coCodigoArticulo}
                </td>
                <td>
                    ${articulo.clpbCategoria.deCategoria}
                </td> 
                <td>
                	<c:choose>
                		<c:when test="${articulo.stManejaStockPuntoCanje eq true}">
                			<fmt:message key='common.maneja.stock.punto.canje'/>
                		</c:when>
                		<c:when test="${articulo.stManejaStockAlmPrincipal eq true}">
                			<fmt:message key='common.maneja.stock.almacen.principal'/>
                		</c:when>                		
                		<c:when test="${articulo.stNoManejaStock eq true}">
                			<fmt:message key='common.no.maneja.stock'/>
                		</c:when>
                		<c:when test="${articulo.stInstantaneoNormal eq true}">
                			<fmt:message key='common.maneja.premioinstantaneo'/>
                		</c:when>
                		<c:when test="${articulo.stInstantaneoVale eq true}">
                			<fmt:message key='common.maneja.premioinstantaneovale'/>
                		</c:when>
                		
                	</c:choose>
                </td>                               
                <td>
                	<c:choose>
                		<c:when test="${articulo.stArticulo eq true}">
                			<fmt:message key='common.yes'/>
                		</c:when>
                		<c:otherwise>
                			<fmt:message key='common.no'/>
                		</c:otherwise>
                	</c:choose>
                </td>
           </tr>
        </c:forEach>				
	</tbody>
</table> 

<c:out value="${buttons}" escapeXml="false" />
</form:form>
<script type="text/javascript">
	
    function buscarArticulos()
    {
        var deArticulo = dwr.util.getValue("deArticulo");
        var coCodigoArticulo = dwr.util.getValue("coCodigoArticulo");
        var coCategoria = dwr.util.getValue("coCategoria");
        var stArticulo = dwr.util.getValue("stArticulo");
        var stManejaStock = dwr.util.getValue("stManejaStock");
       
        dwr.util.useLoadingMessage();
        ArticuloService.buscarArticulos(deArticulo, coCodigoArticulo, coCategoria, stArticulo,
    			stManejaStock, function(articulos) {
        	
    	    // Delete all the rows except for the "pattern" row
    	    dwr.util.removeAllRows("articulosbody", { filter:function(tr) {
    	      return (tr.id != "pattern");
    	    }});
    	    
    	    // Create a new set cloned from the pattern row
    	    var articulo, hrefurl, url, categoria, codigo, stManejaStock, habilitado;
    	    for (var i = 0; i < articulos.length; i++) {
    	    	articulo = articulos[i];

    	      	dwr.util.cloneNode("pattern", { idSuffix:articulo.ID });
    	      	url = "<a href='/clubpgnweb/articulo/articuloform.html?id=" + articulo.ID + "&amp;from=list'>" + articulo.DEARTICULO +	"</a>";	
    	      	dwr.util.setValue("tableArticulo" + articulo.ID, url, { escapeHtml:false });

    	      	codigo    = articulo.COCODIGOARTICULO;
    	      	dwr.util.setValue("tableCodigo" + articulo.ID, codigo, { escapeHtml:false });

    	      	categoria    = articulo.DECATEGORIA;
    	      	dwr.util.setValue("tableCategoria" + articulo.ID, categoria, { escapeHtml:false });


	    	    	stManejaStock = articulo.STINSTANTANEONORMAL;
	    	      	if(stManejaStock == 'Y'){
	    	      		dwr.util.setValue("tableStock" + articulo.ID, '<fmt:message key='common.maneja.premioinstantaneo'/>', { escapeHtml:false }); 
	           	    
		       		}
		    	     stManejaStock = articulo.STINSTANTANEOVALE;
		    	     if(stManejaStock == 'Y'){
		    	      		dwr.util.setValue("tableStock" + articulo.ID, '<fmt:message key='common.maneja.premioinstantaneovale'/>', { escapeHtml:false }); 
		           	}
    	      		stManejaStock = articulo.STMANEJAPUNTOCANJE;
    	      		if(stManejaStock == 'Y'){
    	      		dwr.util.setValue("tableStock" + articulo.ID, '<fmt:message key='common.maneja.stock.punto.canje'/>', { escapeHtml:false }); 
           	    	}
    	      		stManejaStock = articulo.STMANEJASTOCKALMPRINCIPAL;
        	      	if(stManejaStock == 'Y'){
        	      		dwr.util.setValue("tableStock" + articulo.ID, '<fmt:message key='common.maneja.stock.almacen.principal'/>', { escapeHtml:false }); 
               		}
        	      	stManejaStock = articulo.STNOMANEJASTOCK;
            	    if(stManejaStock == 'Y'){
            	      		dwr.util.setValue("tableStock" + articulo.ID, '<fmt:message key='common.no.maneja.stock'/>', { escapeHtml:false }); 
               		}
        	      
		       	
        	   	

    	     	habilitado = articulo.STARTICULO;
    	      	if(habilitado == 'Y'){
    	    	  dwr.util.setValue("tableHabilitado" + articulo.ID, '<fmt:message key='common.yes'/>', { escapeHtml:false }); 
        	  	}
    	      	else {
    	    	  dwr.util.setValue("tableHabilitado" + articulo.ID, '<fmt:message key='common.no'/>', { escapeHtml:false });
        	  	}
    	     
				if(i % 2 == 0){
					$("pattern" + articulo.ID).className = "odd";
				}
				else{
					$("pattern" + articulo.ID).className = "even";
				}
	    	      
	    	    $("pattern" + articulo.ID).style.display = "";
    	    }
		});
  	           		 	
    }       
</script>
<script type='text/javascript' src="<c:url value="/dwr/interface/ArticuloService.js"/>"></script>